﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Gnosis.Alexandria.Data;

namespace Gnosis.Alexandria.Views
{
    public partial class ArtistEditView : WorkView
    {
        public ArtistEditView()
        {
            InitializeComponent();
        }

        private static DateTime GetNormalizedLoadDate(DateTimePicker control, DateTime date)
        {
            if (date < control.MinDate)
                return control.MinDate;
            else if (date > control.MaxDate)
                return control.MaxDate;
            else return date;
        }

        private static DateTime GetNormalizedSaveDate(DateTimePicker control)
        {
            if (control.Value == control.MinDate)
                return DateTime.MinValue;
            else if (control.Value == control.MaxDate)
                return DateTime.MaxValue;
            else return control.Value;
        }

        protected override void HandleMessage(IMessageOld message)
        {
            switch (message.Type)
            {
                case MessageTypes.ResponseRepositoryGetArtist:
                {
                    var data = message.Body as ArtistEditData;
                    Tag = data.Id;
                    txtName.Text = data.Name;
                    txtAbbreviation.Text = data.Abbreviation;
                    txtNote.Text = data.Note;
                    dtStartDate.Value = GetNormalizedLoadDate(dtStartDate, data.StartDate);
                    dtEndDate.Value = GetNormalizedLoadDate(dtEndDate, data.EndDate);

                    cbCountry.DisplayMember = "Name";
                    cbCountry.ValueMember = "Id";
                    cbCountry.DataSource = data.Countries;
                    cbCountry.SelectedValue = data.CountryId;
                    break;
                }
                case MessageTypes.ResponseArtistSaveSucceeded:
                {
                    txtStatus.Text = "Artist saved successfully";
                    break;
                }
                case MessageTypes.ResponseArtistSaveFailed:
                {
                    txtStatus.Text = "There was an error trying to save this Artist";
                    break;
                }
                default:
                    base.HandleMessage(message);
                    break;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var data = new ArtistSaveData
            {
                Id = (long)Tag,
                Name = txtName.Text,
                Abbreviation = txtAbbreviation.Text,
                Note = txtNote.Text,
                CountryId = (long)cbCountry.SelectedValue,
                StartDate = GetNormalizedSaveDate(dtStartDate),
                EndDate = GetNormalizedSaveDate(dtEndDate)
            };
            Controller.Send(this, MessageTypes.RequestArtistEditSave, data);
        }
    }
}
